const { app, BrowserWindow, ipcMain } = require('electron');
const fs = require("fs");
const path = require("path");

//先创建一个demo项目在/data-mock下;创建前需要先检查是否存在demo项目
const demoProjectPath = "/data-mock/projects/demo";
if (!fs.existsSync(demoProjectPath)) {
  fs.mkdirSync(demoProjectPath, { recursive: true });
  fs.copyFileSync(path.resolve(__dirname, 'template/template.js'), path.join(demoProjectPath, 'server.js'));
  fs.copyFileSync(path.resolve(__dirname, 'template/mock.min.js'), path.join(demoProjectPath, 'mock.js'));
  fs.copyFileSync(path.resolve(__dirname, 'demo/demo.json'), path.join(demoProjectPath, 'demo.json'));
}

function createWindow () {
  // 创建浏览器窗口
  let win = new BrowserWindow({
    width: 400,
    height: 600,
    center: true,
    icon: './favicon.ico',
    webPreferences: {
      nodeIntegration: true,
      webviewTag: true
    },
    autoHideMenuBar: true,
    resizable: false,
    minimizable: false
  });

  // 加载index.html文件
  win.loadFile("./views/pages/project-list/index.html");

  global.Window_List_Id = win.id;

  win.on('closed',()=>{ win = null });
}
app.on('ready', createWindow);

// ipcMain.on("createWindow_CreateProject", winId => {
//   global.Window_CreateProject_Id = winId;
// });
// ipcMain.on("createWindow_ProjectDetail", winId => {
//   global.Window_ProjectDetail_Id = winId;
// });

